<template>
  <div class="web_app w h">
    <div class="login_head">
      <img src="../common/images/login.png" alt="">
      <h2>登录云核空间</h2>
    </div>
    <form @submit.prevent="subLogin">
      <div class="login_form">
        <div class="login_line user_top">
          <input type="text" name="userName" v-validate="'required|userName'"
                 :class="{'input': true, 'is-danger': errors.has('userName') }" v-model="userid" placeholder="手机号/邮箱">
        </div>
        <div class="login_err"><span v-show="errors.has('userName')" class="help is-danger">{{ errors.first('userName')
          }}</span>
        </div>
        <div class="login_line">
          <input type="password" name="passwordVal" v-validate="'required|passwordVal'"
                 :class="{'input': true, 'is-danger': errors.has('passwordVal') }" v-model="password"
                 placeholder="请输入密码">
        </div>
        <div class="login_err">
          <span v-show="errors.has('passwordVal')" class="help is-danger">{{ errors.first('passwordVal') }}</span>
          <span>{{errMsg}}</span>
        </div>
        <div class="login_href">
          <!--<a class="login_href fr" href="javascript:;">忘记密码?</a>-->
        </div>
        <button class="button login_btn">登录</button>
        <div class="login_desc">没有账号吗?立即
          <!--<router-link tag="a" to="/reg">注册</router-link>-->
          <a :href="regUrl+'/web-views/register.html'">注册</a>
        </div>
        <div class="go_reg">
          <!--<router-link tag="a" to="/reg"><span></span>注册领红包</router-link>-->
          <!--<a href="#"><span></span>注册领红包></a>-->
        </div>
      </div>
    </form>
  </div>
</template>

<script type="text/ecmascript-6">
  import md5 from 'js-md5'
  import * as types from '../store/types'
  import {Toast} from 'mint-ui'

  import {ERR_OK,PCHOST} from 'config/config'

  export default {
    data() {
      return {
        userid: '',
        password: '',
        errMsg: '',
        regUrl:PCHOST
      }
    },
    mounted() {
//      this.$store.dispatch('showLoading');
    },
    methods: {
      subLogin() {
        this.$validator.validateAll({
          userName: this.userid,
          passwordVal: this.password
        }).then((result) => {
          if (!result) {
            return
          }

          return this.$http({
            method: 'post',
            url: '/mobile/api/v1.0/user/login',
            data: {
              userid: this.userid,
              password: md5(this.password)
            }
          })
        }).then((res) => {
          if (res.data.code == ERR_OK) {
            Toast('登录成功！')
            this.errMsg = '';
            this.$store.commit(types.LOGIN, res.data.data.token);
            let redirect = decodeURIComponent(this.$route.query.redirect || '/');
            this.$router.push({
              path: '/main'
            })
          } else {
            this.errMsg = res.data.msg;
          }

        }).catch(function (err) {
          console.log(err)
        })
      }

    }
  }
</script>

<style lang="less" rel="stylesheet/less">

</style>
